package LeetCode;

import java.util.Scanner;
import java.util.Stack;

public class LC_020_ValidParentheses {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s = sc.nextLine();
            System.out.println(isValid(s));
        }
        sc.close();
    }

    public static boolean isValid(String s) {
        if (s.length() == 0)
            return false;
        if (s.length() == 1)
            return false;

        Stack<Character> stk = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(') {
                stk.push(c);
            } else if (c == '[') {
                stk.push(c);
            } else if (c == '{') {
                stk.push(c);
            } else if (c == ')' && !stk.isEmpty() && stk.peek() == '(') {
                stk.pop();
            } else if (c == ']' && !stk.isEmpty() && stk.peek() == '[') {
                stk.pop();
            } else if (c == '}' && !stk.isEmpty() && stk.peek() == '{') {
                stk.pop();
            }
        }

        return stk.isEmpty();
    }
}
